<template>
    <el-dialog
        :before-close="_close"
        :visible.sync="propValue"
        title="选择角色"
        width="30%">
        <el-tree
            ref="menu"
            empty-text="暂无配置"
            default-expand-all
            :data="treeData"
            :default-checked-keys="permissions"
            :props="{label:'name'}"
            node-key="id"
            show-checkbox
        ></el-tree>
        <span slot="footer" class="dialog-footer">
            <el-button size="medium" type="primary" @click="onSubmit">保存</el-button>
            <el-button size="medium" @click="_close">取消</el-button>
        </span>
    </el-dialog>
</template>
<script>

import {getRoleIdByUserId, userAddRole} from "@/api/userRole";
import {findAll, findAllNoPage} from "@/api/role";

export default {
    name: 'rolePopup',
    components: {},
    props: {
        value: {
            type: Boolean,
            default: false,
        },
        currentData: {
            type: Object,
            default() {
                return {}
            },
        },
    },
    data() {
        return {
            activeName: '1',
            treeData: [],
            permissions: [],
            checkStrictly: true,
        }
    },
    computed: {
        propValue: {
            get() {
                return this.value
            },
            set(nval) {
                this.$emit('input', nval)
            },
        },
    },
    methods: {
        async getTree() {
            let {code, data} = await this.$api.permission.role.findAll()
            if (code === 200) {
                await this.getRolePermissions(data)
            }
        },

        async getRolePermissions(treeData) {
            let params = {
                userId: this.currentData.id,
            }
            let {code, data} = await this.$api.permission.userRole.getRoleIdByUserId(params)
            if (code === 200) {
                this.permissions = data
                this.treeData = treeData
            }
        },
        async onSubmit() {
            let params = {
                userId: this.currentData.id,
                roleIds: this.$refs.menu.getCheckedKeys().concat(this.$refs.menu.getHalfCheckedKeys()),
            }

            let {code, msg} = await this.$api.permission.userRole.userAddRole(params)
            if (code === 200) {
                this.$message.success('配置成功' || msg)
                this.$emit('input', false)
                this.$emit('success')
            }
        },
        //关闭弹窗
        _close() {
            this.treeData = []
            this.permissions = []
            this.$emit('input', false)
        },
    },
}
</script>
<style lang="scss" scoped>

</style>
